package com.CST.service.impl.ys;

import com.CST.dto.ys.OrganizationDTO;
import com.CST.entity.ys.YsOrganizationInfo;
import com.CST.mapper.cst.ys.YsOrganizationInfoMapper;
import com.CST.mapper.cst.ys.YsSubjectMapper;
import com.CST.service.ys.YsOrganizationInfoService;
import com.CST.util.ExcelTransfer;
import com.CST.util.PageHelperTool;
import com.CST.vo.ys.OrganizationInfoVO;
import com.CST.vo.ys.OrganizationVO;
import com.CST.vo.ys.YsSubjectVO;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  预算编制数据服务实现类
 *
 * @author cw
 * @since 2023-05-15
 */
@Service
public class YsOrganizationInfoServiceImpl extends ServiceImpl<YsOrganizationInfoMapper, YsOrganizationInfo> implements YsOrganizationInfoService {

    private final ExcelTransfer<YsOrganizationInfo> excelImport;

    @Autowired
    private YsSubjectMapper ysSubjectMapper;

    public YsOrganizationInfoServiceImpl(ExcelTransfer<YsOrganizationInfo> excelImport) {
        this.excelImport = excelImport;
    }

    /**
     * 获取业务职能预算编制列表
     * @param dto 查询条件
     * @return 业务数据列表
     */
    @Override
    public OrganizationVO getBusinessOrganizationInfoList(OrganizationDTO dto) {
        OrganizationVO result = new OrganizationVO();
        List<OrganizationInfoVO> resultList = baseMapper.selectBusinessOrganizationList(dto);

        List<Integer> monthList = resultList.stream().map(OrganizationInfoVO::getMonth).collect(Collectors.toList());
        monthList = monthList.stream().distinct().collect(Collectors.toList());

        result.setInfoList(PageHelperTool.excutePageInfo(resultList, dto.getPageNum(), dto.getPageSize()));
        result.setMonthList(monthList);
        return result;
    }

    /**
     * 获取管理职能预算编制列表
     * @param dto 查询条件
     * @return 管理数据列表
     */
    @Override
    public OrganizationVO getManageOrganizationInfoList(OrganizationDTO dto) {
        OrganizationVO result = new OrganizationVO();
        List<OrganizationInfoVO> resultList = baseMapper.selectManageOrganizationList(dto);

        List<Integer> monthList = resultList.stream().map(OrganizationInfoVO::getMonth).collect(Collectors.toList());
        monthList = monthList.stream().distinct().collect(Collectors.toList());

        result.setInfoList(PageHelperTool.excutePageInfo(resultList, dto.getPageNum(), dto.getPageSize()));
        result.setMonthList(monthList);
        return result;
    }

    /**
     * 下载模版
     * @param response 模版
     */
    @Override
    public void download(HttpServletResponse response) {
        // 查询出所有的预算科目信息--只查询出所有的管理职能
        List<YsSubjectVO> list = ysSubjectMapper.selectThirstChild(5);
        writeExcel(response,list);
    }

    /**
     * 导入
     * @param file 文件参数
     */
    @Override
    public void importExcel(MultipartFile file) {
        excelImport.importExcel(file,this, YsOrganizationInfo.class);
    }

    private void writeExcel(HttpServletResponse response, List<YsSubjectVO> list) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = "dict";
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

            //调用方法进行写操作
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
            //获取sheet0对象
            WriteSheet mainSheet = EasyExcel.writerSheet(0, "业务职能预算编制数据").head(OrganizationInfoVO.class).build();
            List<OrganizationInfoVO> voList = new ArrayList<>();
            list.stream().forEach(s->{
                OrganizationInfoVO vo = new OrganizationInfoVO();
                vo.setSubjectId(s.getId());
                vo.setSubjectCode(s.getSubjectCode());
                vo.setSubjectName(s.getSubjectName());
                voList.add(vo);
            });
            excelWriter.write(voList,  mainSheet);

            //关闭流
            excelWriter.finish();
        } catch (Exception e) {
            throw new RuntimeException("下载失败");
        }
    }
}
